<?php
namespace app\api\controller;
use app\wechat\service\JwtService;
use Decimal\Decimal;
use think\admin\Controller;
use think\Request;
use think\facade\Db;
use think\Validate;


/**
 * 前端地址管理
 */
class Address extends Controller
{
    
     //创建新地址
    public function create_address()
    {
        $param = $this->request->param();
        $validate = new Validate([
            'ad1' => 'require',
            'ad2' => 'require',
            'ad3' => 'require',
            'detail' => 'require',
            'rec_user_name' => 'require',
            'rec_mobile' => 'require|mobile'
        ], [
            'ad1.require' => '省份信息不能为空',
            'ad2.require' => '市信息不能为空',
            'ad3.require' => '区县信息不能为空',
            'detail.require' => '详细地址不能为空',
            'rec_user_name.require' => '收货人姓名不能为空',
            'rec_mobile.require' => '收货人手机号不能为空',
            'rec_mobile.mobile' => '收货人手机号格式错误'
        ]);

        $check_res = $validate->check($param);
        if (!$check_res) {
            return xcx_error($validate->getError());
        }
        if(empty($param['user_id'])){
            return xcx_error('用户信息获取失败！');
        }
        $address_count = Db::name('user_address')->where(['user_id' => $param['user_id'], 'status' => 1])->count();
        if ($address_count >= 50) {
            return xcx_error('地址数量已达上限，请删除不常用地址后再添加');
        }

        $param['user_id'] = $param['user_id'];
        $param['status'] = 1;
        $param['create_time'] = $param['update_time'] = date('Y-m-d');
        unset($param['payload'],$param['version'],$param['uid'],$param["openid"]);
        $address_id = Db::name('user_address')->insertGetId($param);
        if ($address_id) {
            return xcx_success('地址新增成功');
        } else {
            return xcx_error('地址新增失败');
        }
    }


    //更新地址信息
    public function update_address()
    {
        $param = $this->request->param();
        $validate = new Validate([
            'address_id' => 'require',
            'ad1' => 'require',
            'ad2' => 'require',
            'ad3' => 'require',
            'detail' => 'require',
            'rec_user_name' => 'require',
            'rec_mobile' => 'require|mobile'
        ], [
            'ad1.require' => '省份信息不能为空',
            'ad2.require' => '市信息不能为空',
            'ad3.require' => '区县信息不能为空',
            'detail.require' => '详细地址不能为空',
            'rec_user_name.require' => '收货人姓名不能为空',
            'rec_mobile.require' => '收货人手机号不能为空',
            'rec_mobile.mobile' => '收货人手机号格式错误'
        ]);

        $check_res = $validate->check($param);
        if (!$check_res) {
            return xcx_error($validate->getError());
        }
        $address_id = $param['address_id'];
        $param['user_id'] = $param['user_id'];
        $param['update_time'] = date('Y-m-d');
        unset($param['payload'], $param['address_id'],$param['version'],$param['user_id'],$param["openid"]);
        $address_id = Db::name('user_address')->where(['id' => $address_id])->update($param);
        if ($address_id) {
            return xcx_success("地址更新成功");
        } else {
            return xcx_success("地址更新成功");
        }
    }


    //分页获取用户地址列表
    public function get_address_list()
    {
        $page = input('page', 1);
        $page_size = input('page_size', 10);
        $payload = input('payload');
        $uid= input('user_id');
        $address_list = Db::name('user_address')->where(['user_id' => $uid, 'status' => 1])
        ->page($page)
        ->limit($page_size)
        ->select();
        if ($address_list) {
            return xcx_success($address_list);
        } else {
            return xcx_error('地址列表为空');
        }
    }

    //获取单条地址信息
    public function get_address_byone()
    {
        $param = $this->request->param();
        $validate = new Validate([
            'id' => 'require|number'
        ]);

        $check_res = $validate->check($param);
        if (!$check_res) {
            return xcx_error($validate->getError());
        }
        $user_id = input('user_id');
        $address_list = Db::name('user_address')->where(['user_id' => $user_id, 'status' => 1, "id" => $param['id']])->find();
        if ($address_list) {
            return xcx_success($address_list);
        } else {
            return xcx_error('地址列表为空');
        }
    }
}